package laMalinche.testingPlayGround.aspect;

import laMalinche.LaMaContext;

import java.beans.PropertyChangeSupport;
import java.beans.PropertyChangeListener;
import java.util.Map;
import java.util.WeakHashMap;

/**
 * Created by IntelliJ IDEA.
 * User: ranran
 * Date: Aug 10, 2009
 * Time: 9:20:08 PM
 * To change this template use File | Settings | File Templates.
 */
public class PropertyChangeSupportMap {
    private  static PropertyChangeSupportMap instance = new PropertyChangeSupportMap();

    Map<Object, PropertyChangeSupport> propertyChangeSupportMap =  new WeakHashMap<Object, PropertyChangeSupport>();

    public PropertyChangeSupportMap() {

    }

    public static PropertyChangeSupportMap getInstance() {

        return instance;

    }

    private PropertyChangeSupport getPropertyChangeSupport(Object o){

        if (!propertyChangeSupportMap.containsKey(o)){

            PropertyChangeSupport value = new PropertyChangeSupport(o);

            PropertyChangeListener propertyChangeListener = (PropertyChangeListener) LaMaContext.getInstance().getAccessReporter();

            if (propertyChangeListener == null){

                throw new RuntimeException("AccessReporter not initialized yet");

            }

            value.addPropertyChangeListener(propertyChangeListener);

            propertyChangeSupportMap.put(o, value);

        }

        return propertyChangeSupportMap.get(o);

    }


    public void fireAccessReport(Object o, String fieldName){

        getPropertyChangeSupport(o).firePropertyChange(fieldName,"1","");

    }
}
